namespace DotNetNuke.UI.Skins
{
    using DotNetNuke.Common;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// SkinThumbNailControl is a user control that provides that displays the skins
    /// as a Radio ButtonList with Thumbnail Images where available
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	10/12/2004	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public abstract class SkinThumbNailControl : UserControlBase
    {
        private int _Columns;
        //("ControlContainer")]
        private HtmlGenericControl _ControlContainer;
        //("optSkin")]
        private RadioButtonList _optSkin;

        public SkinThumbNailControl()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
            this._Columns = -1;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddDefaultSkin adds the not-specified skin to the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	12/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddDefaultSkin()
        {
            string strDefault = (DotNetNuke.Services.Localization.Localization.GetString("Not_Specified") + "<br>") + "<img src=\"" + DotNetNuke.Common.Globals.ApplicationPath + "/images/spacer.gif\" width=\"140\" height=\"135\" border=\"0\">";
            this.optSkin.Items.Insert(0, new ListItem(strDefault, ""));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// AddSkin adds the skin to the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="strFolder">The Skin Folder</param>
        /// <param name="strFile">The Skin File</param>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void AddSkin(string root, string strFolder, string strFile)
        {
            string strImage = "";
            if (File.Exists(strFile.Replace(".ascx", ".jpg")))
            {
                strImage = strImage + "<a href=\"" + this.CreateThumbnail(strFile.Replace(".ascx", ".jpg")).Replace("thumbnail_", "") + "\" target=\"_new\"><img src=\"" + this.CreateThumbnail(strFile.Replace(".ascx", ".jpg")) + "\" border=\"1\"></a>";
            }
            else
            {
                strImage = strImage + "<img src=\"" + DotNetNuke.Common.Globals.ApplicationPath + "/images/thumbnail.jpg\" border=\"1\">";
            }
            this.optSkin.Items.Add(new ListItem(this.FormatSkinName(strFolder, Path.GetFileNameWithoutExtension(strFile)) + "<br>" + strImage, root + "/" + strFolder + "/" + Path.GetFileName(strFile)));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Clear clears the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	12/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void Clear()
        {
            this.optSkin.Items.Clear();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateThumbnail creates a thumbnail of the Preview Image
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="strImage">The Image File Name</param>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private string CreateThumbnail(string strImage)
        {
            bool blnCreate = true;
            string strThumbnail = strImage.Replace(Path.GetFileName(strImage), "thumbnail_" + Path.GetFileName(strImage));
            if (File.Exists(strThumbnail))
            {
                DateTime d1 = File.GetLastWriteTime(strThumbnail);
                DateTime d2 = File.GetLastWriteTime(strImage);
                if (DateTime.Compare(File.GetLastWriteTime(strThumbnail), File.GetLastWriteTime(strImage)) == 0)
                {
                    blnCreate = false;
                }
            }
            if (blnCreate)
            {
                int intSize = 140;
                try
                {
                    double dblScale;
                    int intHeight;
                    int intWidth;
                    System.Drawing.Image objImage = System.Drawing.Image.FromFile(strImage);
                    if (objImage.Height > objImage.Width)
                    {
                        dblScale = ((double) intSize) / ((double) objImage.Height);
                        intHeight = intSize;
                        intWidth = (int) Math.Round((double) (objImage.Width * dblScale));
                    }
                    else
                    {
                        dblScale = ((double) intSize) / ((double) objImage.Width);
                        intWidth = intSize;
                        intHeight = (int) Math.Round((double) (objImage.Height * dblScale));
                    }
                    System.Drawing.Image objThumbnail = objImage.GetThumbnailImage(intWidth, intHeight, null, IntPtr.Zero);
                    if (File.Exists(strThumbnail))
                    {
                        File.Delete(strThumbnail);
                    }
                    objThumbnail.Save(strThumbnail, objImage.RawFormat);
                    File.SetAttributes(strThumbnail, FileAttributes.Normal);
                    File.SetLastWriteTime(strThumbnail, File.GetLastWriteTime(strImage));
                    objImage.Dispose();
                    objThumbnail.Dispose();
                }
                catch (Exception exception1)
                {
                    
                    
                }
            }
            return (DotNetNuke.Common.Globals.ApplicationPath + @"\" + strThumbnail.Substring(strThumbnail.ToLower().IndexOf(@"portals\")));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// format skin name
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="strSkinFolder">The Folder Name</param>
        /// <param name="strSkinFile">The File Name without extension</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        private string FormatSkinName(string strSkinFolder, string strSkinFile)
        {
            if (strSkinFolder.ToLower() == "_default")
            {
                return strSkinFile;
            }
            string refStringHelperL0 = strSkinFile.ToLower();
            if ((refStringHelperL0 == "skin") || (refStringHelperL0 == "container"))
            {
            }
            if (((refStringHelperL0 == "default") ? 1 : 0) != 0)
            {
                return strSkinFolder;
            }
            return (strSkinFolder + " - " + strSkinFile);
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadAllSkins loads all the available skins (Host and Site) to the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="includeNotSpecified">Optionally include the "Not Specified" option</param>
        /// <history>
        /// [cnurse]	12/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void LoadAllSkins(bool includeNotSpecified)
        {
            if (includeNotSpecified)
            {
                this.AddDefaultSkin();
            }
            this.LoadHostSkins(false);
            this.LoadPortalSkins(false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadHostSkins loads all the available Host skins to the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="includeNotSpecified">Optionally include the "Not Specified" option</param>
        /// <history>
        /// [cnurse]	12/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void LoadHostSkins(bool includeNotSpecified)
        {
            if (includeNotSpecified)
            {
                this.AddDefaultSkin();
            }
            string strRoot = DotNetNuke.Common.Globals.HostMapPath + this.SkinRoot;
            if (Directory.Exists(strRoot))
            {
                foreach (string strFolder in Directory.GetDirectories(strRoot))
                {
                    if (!strFolder.EndsWith("_default"))
                    {
                        this.LoadSkins(strFolder, "[G]", false);
                    }
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadHostSkins loads all the available Site/Portal skins to the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="includeNotSpecified">Optionally include the "Not Specified" option</param>
        /// <history>
        /// [cnurse]	12/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void LoadPortalSkins(bool includeNotSpecified)
        {
            if (includeNotSpecified)
            {
                this.AddDefaultSkin();
            }
            string strRoot = this.PortalSettings.HomeDirectoryMapPath + this.SkinRoot;
            if (Directory.Exists(strRoot))
            {
                foreach (string strFolder in Directory.GetDirectories(strRoot))
                {
                    this.LoadSkins(strFolder, "[L]", false);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSkins loads all the available skins in a specific folder to the radio button list
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="strFolder">The folder to search for skins</param>
        /// <param name="skinType">A string that identifies whether the skin is Host "[G]" or Site "[L]"</param>
        /// <param name="includeNotSpecified">Optionally include the "Not Specified" option</param>
        /// <history>
        /// [cnurse]	12/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void LoadSkins(string strFolder, string skinType, bool includeNotSpecified)
        {
            if (includeNotSpecified)
            {
                this.AddDefaultSkin();
            }
            if (Directory.Exists(strFolder))
            {
                string[] arrFiles = Directory.GetFiles(strFolder, "*.ascx");
                strFolder = Strings.Mid(strFolder, Strings.InStrRev(strFolder, @"\", -1, CompareMethod.Binary) + 1);
                foreach (string strFile in arrFiles)
                {
                    this.AddSkin(skinType + this.SkinRoot, strFolder, strFile);
                }
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	10/12/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, EventArgs e)
        {
        }

        public string Border
        {
            get
            {
                return Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinControlBorder"]));
            }
            set
            {
                this.ViewState["SkinControlBorder"] = value;
                if (value != "")
                {
                    this.ControlContainer.Style.Add("border-top", value);
                    this.ControlContainer.Style.Add("border-bottom", value);
                    this.ControlContainer.Style.Add("border-left", value);
                    this.ControlContainer.Style.Add("border-right", value);
                }
            }
        }

        public int Columns
        {
            get
            {
                return Convert.ToInt32(RuntimeHelpers.GetObjectValue(this.ViewState["SkinControlColumns"]));
            }
            set
            {
                this.ViewState["SkinControlColumns"] = value;
                if (value > 0)
                {
                    this.optSkin.RepeatColumns = value;
                }
            }
        }

        protected virtual HtmlGenericControl ControlContainer
        {
           
            get
            {
                return this._ControlContainer;
            }
            
            set
            {
                this._ControlContainer = value;
            }
        }

        public string Height
        {
            get
            {
                return Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinControlHeight"]));
            }
            set
            {
                this.ViewState["SkinControlHeight"] = value;
                if (value != "")
                {
                    this.ControlContainer.Style.Add("height", value);
                }
            }
        }

        protected virtual RadioButtonList optSkin
        {
           
            get
            {
                return this._optSkin;
            }
            
            set
            {
                this._optSkin = value;
            }
        }

        public string SkinRoot
        {
            get
            {
                return Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinRoot"]));
            }
            set
            {
                this.ViewState["SkinRoot"] = value;
            }
        }

        public string SkinSrc
        {
            get
            {
                if (this.optSkin.SelectedItem != null)
                {
                    return this.optSkin.SelectedItem.Value;
                }
                return "";
            }
            set
            {
                int refIntHelperL0 = this.optSkin.Items.Count - 1;
                for (int intIndex = 0; intIndex <= refIntHelperL0; intIndex++)
                {
                    if (this.optSkin.Items[intIndex].Value == value)
                    {
                        this.optSkin.Items[intIndex].Selected = true;
                        break;
                    }
                }
            }
        }

        public string Width
        {
            get
            {
                return Convert.ToString(RuntimeHelpers.GetObjectValue(this.ViewState["SkinControlWidth"]));
            }
            set
            {
                this.ViewState["SkinControlWidth"] = value;
                if (value != "")
                {
                    this.ControlContainer.Style.Add("width", value);
                }
            }
        }
    }
}

